/**
 * 项目名称：EIMClient
 * 类 名 称：MeetingMemItemView
 * 类 描 述：(描述信息)
 * 创 建 人：CB
 * 创建时间：2015-1-19 下午1:34:03
 * 修 改 人：CB
 * 修改时间：2015-1-19 下午1:34:03
 * 修改备注：
 * @version
 * 
*/
package com.yinhai.view;


import android.content.Context;
import android.content.res.TypedArray;
import android.text.TextUtils.TruncateAt;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.littlec.sdk.CMChatConfig;
import com.yinhai.activity.R;
import com.yinhai.model.CMMeetingContact;
import com.yinhai.util.CommonUtils;

/**
 * @包名：com.littlec.chatdemo.view
 * @类名：MeetingMemItemView
 * @描述：(描述这个类的作用)
 * @作者：CB
 * @时间：2015-1-19下午1:34:03
 * @版本：1.0.0
 * 
 */
public class MeetingMemItemView extends LinearLayout {

	private  Context  mContext=null;
	private  int mType=LayoutType.LAYOUT_TYPE_ITOP_TBOTTOM;
	private  ImageView avatar=null;
	private  TextView  name=null;
	private  CMMeetingContact user=null;
	public   static class LayoutType{
		     /**图片下，文本上*/
	         public static final  int	 LAYOUT_TYPE_TTOP_IBOTTOM=0;
		     /**图片左，文本右*/
	         public static final  int	 LAYOUT_TYPE_ILEFT_TRIGHT=1;
		     /**图片右，文本左*/
	         public static final  int    LAYOUT_TYPE_TLEFT_IRIGHT=2;
	         /**图片上，文本下*/
	         public static final  int	 LAYOUT_TYPE_ITOP_TBOTTOM=3;
		     /**只显示图片*/
	         public static final  int    LAYOUT_TYPE_TGONE=4;
	}
	
	
	public MeetingMemItemView(Context context) {
		super(context);
		this.mContext=context;
		initView(null);
	}
	
	public MeetingMemItemView(Context context,int layoutType) {
		super(context);
		this.mContext=context;
		this.mType=layoutType;
		initView(null);
	}
	
	public MeetingMemItemView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		this.mContext=context;
		initView(attrs);
	}
	
	public MeetingMemItemView(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.mContext=context;
		initView(attrs);
	}
	
	public void setCMMeetingContact(CMMeetingContact user){
		if(user==null) return;
		this.user=user;
		String _name=user.getName();
		if(_name.endsWith("#"+CMChatConfig.appKey))//name is null,real is username
			_name=_name.replace("#"+CMChatConfig.appKey,"");
		name.setText(_name);
		int _status=user.getMeetingStatus();
		if(_status==2)
			setMemberStatus(true);  //点亮
		else
		    setMemberStatus(false);  //默认未入会，灰色
	}
	
	public CMMeetingContact getCMMeetingContact(){
		return this.user;
	}
	
	public void setMemberStatus(boolean joined){
		if(joined){
			avatar.setImageResource(R.drawable.meeting_member_joined);
			name.setTextColor(mContext.getResources().getColor(R.color.cor1));
		}else{
			avatar.setImageResource(R.drawable.meeting_member_no_joined);
			name.setTextColor(mContext.getResources().getColor(R.color.cor3));
		}
	}
	
	private  void initView(AttributeSet attrs){
        if(attrs!=null){
        	TypedArray a = mContext.obtainStyledAttributes(attrs, R.styleable.MeetingMemItemView);
        	mType = a.getInt(R.styleable.MeetingMemItemView_LayoutType,LayoutType.LAYOUT_TYPE_ITOP_TBOTTOM);
        }
        
		avatar=new ImageView(mContext);
		avatar.setImageResource(R.drawable.meeting_member_no_joined);//没有人
		avatar.setScaleType(ScaleType.CENTER_CROP);
		avatar.setBackgroundResource(0);
		name=new TextView(mContext);
		name.setGravity(Gravity.CENTER);
		name.setEllipsize(TruncateAt.END);
		name.setMaxWidth(CommonUtils.dip2px(mContext,65));
		name.setTextSize(CommonUtils.dip2px(mContext,6));
		name.setBackgroundResource(0);
		name.setSingleLine(true);

		name.setTextColor(mContext.getResources().getColor(R.color.cor3));
		this.setGravity(Gravity.CENTER);
		
		LinearLayout.LayoutParams lp=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
        switch(mType) {
			case LayoutType.LAYOUT_TYPE_TTOP_IBOTTOM:
				setOrientation(VERTICAL);
				this.addView(name,LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
				lp.topMargin=CommonUtils.dip2px(mContext,5);
				this.addView(avatar,lp);
				break;
            case LayoutType.LAYOUT_TYPE_ILEFT_TRIGHT:
            	setOrientation(HORIZONTAL);
        		this.addView(avatar);
        		lp.leftMargin=CommonUtils.dip2px(mContext,5);
        		this.addView(name,lp);
				break;
            case LayoutType.LAYOUT_TYPE_TLEFT_IRIGHT:
            	setOrientation(HORIZONTAL);
        		this.addView(name);
        		lp.leftMargin=CommonUtils.dip2px(mContext,5);
        		this.addView(avatar,lp);
				break;
            case LayoutType.LAYOUT_TYPE_ITOP_TBOTTOM:
				setOrientation(VERTICAL);
				this.addView(avatar,LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
				lp.topMargin=CommonUtils.dip2px(mContext,5);
				this.addView(name,lp);
				break;
            case LayoutType.LAYOUT_TYPE_TGONE:
            	setOrientation(VERTICAL);
				this.addView(avatar);
				this.addView(name);
				name.setVisibility(View.GONE);
	            break;
			default:
				break;
		}
	}
	
}
